package com.xy.biz.mes.domain.bo;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 *
 *
 * @author zsp
 * @date 2022-1-20
 */
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
@Data
public class ManufactureOrderGoodsBO {

    /**
     * 生产订单货品明细Id
     */
    private Long manufactureOrderGoodsId;
    /**
     * 生产订单Id
     */
    private Long manufactureOrderId;
    /**
     * 生产订单编号
     */
    private String manufactureOrderCode;
    /**
     * 类型：1-产品，2-原料
     */
    private Integer type;
    /**
     * 生产货品Id
     */
    private Long goodsId;
    /**
     * 生产货品货号
     */
    private String goodsCode;
    /**
     * 生产货品识别码
     */
    private String goodsFullCode;
    /**
     * 生产货品简称
     */
    private String goodsAbbr;
    /**
     * 生产货品单位
     */
    private String unit;
    /**
     * 生产货品规格
     */
    private Double specification;
    /**
     * 生产货品批次Id
     */
    private Long goodsBatchId;
    /**
     * 生产货品批次号
     */
    private String goodsBatchCode;
    /**
     * 生产货品批次浓度
     */
    private String concentration;
    /**
     * 生产货品批次浓度单位
     */
    private String concentrationUnit;
    /**
     * 生产日期
     */
    private java.time.LocalDate productionDate;
    /**
     * 到期日期
     */
    private java.time.LocalDate expirationDate;
    /**
     * 计划个数
     */
    private Integer expectCount;
    /**
     * 计划数量
     */
    private Double expectQuantity;
    /**
     * 实际个数
     */
    private Integer factCount;
    /**
     * 实际数量
     */
    private Double factQuantity;
    /**
     * 损耗率
     */
    private String attritionRate;
    /**
     * 重量
     */
    private Double weight;
    /**
     * 重量单位
     */
    private String weightUnit;
    /**
     * 租户Id，0表示不指定
     */
    private Long tenantId;

    public void clearExpectId(ManufactureOrderGoodsBO manufactureOrderGoodsBO) {
        Objects.requireNonNull(manufactureOrderGoodsBO);
        long id = manufactureOrderGoodsBO.getManufactureOrderGoodsId();
        Class<? extends ManufactureOrderGoodsBO> clazz = manufactureOrderGoodsBO.getClass();
        Field[] declaredFields = clazz.getDeclaredFields();
        try {
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                declaredField.set(manufactureOrderGoodsBO, null);
            }
        } catch (IllegalAccessException e) {
            log.error(e.getMessage());
        }
        manufactureOrderGoodsBO.setManufactureOrderGoodsId(id);
    }

    public ManufactureOrderGoodsBO calculateAttritionRate(ManufactureOrderGoodsBO manufactureOrderGoodsBO) {
        Objects.requireNonNull(manufactureOrderGoodsBO.getExpectQuantity(), "此任务 产品/物料 计划数量为空：" + manufactureOrderGoodsBO.getManufactureOrderGoodsId());
        double expectQuantity = manufactureOrderGoodsBO.getExpectQuantity();
        double factQuantity = Objects.isNull(manufactureOrderGoodsBO.getFactQuantity()) ? 0 : manufactureOrderGoodsBO.getFactQuantity();
        double attrition = expectQuantity - factQuantity;
        String attritionRate;
        if (attrition > 0) {
            attritionRate = String.format("%.2f", ((attrition / expectQuantity) * 100)) + "%";
        } else {
            attritionRate = "0%";
        }
        manufactureOrderGoodsBO.setAttritionRate(attritionRate);
        return this;
    }
}
